package com.zx.scp.controller;

import com.zx.map.utils.CommonResponseUtils;
import com.zx.map.utils.MyBeanUtils;
import com.zx.map.utils.SUConstants;
import com.zx.platform.utils.HttpParamsUtils;
import com.zx.scp.service.IIncomeService;
import com.zx.scp.service.model.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 连连对接
 * Created by IntelliJ IDEA. User: Administrator Date: 15-6-7 Time: 上午1:08 To
 * change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/income/yintong")
public class YinTongIncomeController {
    /**
     * log
     */
    private static Logger log = Logger.getLogger(YinTongIncomeController.class);

    @Autowired
    @Qualifier("yinTongIncomeServiceImpl")
    private IIncomeService incomeService;

    @RequestMapping("/startPay")
    @ResponseBody
    public Map startPay(YintongPayRequestModel yintongPayRequestModel, Model model,
                              HttpServletRequest request) {
        try {
            return incomeService.startPay(request,yintongPayRequestModel);
        } catch (Exception e) {
            log.error("获取二维码异常", e);
            return CommonResponseUtils.failureResponse(SUConstants.SERVER_ERROR);
        }
    }

    /**
     * 微信公众号支付
     * @param orderResponseDomain
     * @param request
     * @return
     */
    @RequestMapping("/winxinJsPay")
    @ResponseBody
    public Map winxinJsPay(YintongPayRequestModel orderResponseDomain,HttpServletRequest request){
        try {
            return  incomeService.winxinJsPay(request,orderResponseDomain);
        } catch (Exception e) {
            e.printStackTrace();
            log.debug("威富通获取支付信息异常",e);
            return CommonResponseUtils.failureResponse(SUConstants.SERVER_ERROR);
        }
    }
    @RequestMapping("/nofityPay")
    @ResponseBody
    public Map notifyPay( HttpServletRequest request) {
        try {
             String str= HttpParamsUtils.getParameterStr(request);
            log.info("订单通知返回：" + str);
            return incomeService.payNotify(MyBeanUtils.jsonToBean(str,Map.class));
        } catch (Exception e) {
            log.error("订单通知返回处理异常", e);
            Map map=new HashMap();
            map.put("ret_code","9000");//处理成功
            map.put("ret_msg",SUConstants.SERVER_ERROR);
            return map;
        }
    }
}
